package es.ats.codington.festival.utils;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.io.*;

import es.ats.codington.festival.exceptions.DAOException;


/**
 * 
 * @author Codington_RED
 *
 */
public class FERSDataConnection {
	/*
	//File f = new File("");
	File fichero = new File("JsonConnection.json");
	
	
	
	public FERSDataConnection(File fichero) {
		super();
		this.fichero = fichero;
		System.out.println (fichero.getAbsolutePath());
	}*/

	private static Connection conn;
	//private static final JsonTrial jsonprueba = new JsonTrial("src/es/ats/codington/festival/json/JsonConnection.json");
	

	/**
	 * establishes a connection with Database
	 * @return conn
	 * @throws DAOException
	 */
	public static Connection getConnection() throws DAOException{
			 
		
		try{
			
			
			//Loading the driver
			//Class.forName(jsonprueba.readString("Driver")).newInstance();
			Class.forName("com.mysql.jdbc.Driver");
			
			//Obtaining a connection using DriverManager class
			//conn = DriverManager.getConnection(jsonprueba.readString("url"),jsonprueba.readString("user"),jsonprueba.readString("pass"));
			conn = DriverManager.getConnection("jdbc:mysql://localhost/codingtonred", "root", "abcd1234");
			conn.setAutoCommit(false);
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		return conn;
	}
	/**
	 * method used to close the connection
	 * @param conn
	 * @throws DAOException
	 */
	public static void closeConnection(Connection conn) throws DAOException{
		
		if(conn != null){
			try {
				conn.close();
				
			} catch(SQLException ex) {
				throw new DAOException(ex.getMessage(), ex);
				
			}
		}
	}
	
	/**
	 * method used to actualize the database.
	 * @param conn
	 * @throws DAOException
	 */
	public static void commit(Connection conn) throws DAOException{
			
		 if(conn != null) {
			 try {
				 conn.commit();
				
			 } catch(SQLException ex) { 
				 throw new DAOException(ex.getMessage(), ex);
				
			 }
		 }
	 }
	 /**
	  * method used to actualize the database but finally erasing all changes that have been done
	  * @param conn
	  * @throws DAOException
	  */
	 public static void rollback(Connection conn) throws DAOException{
		 
		 if(conn != null){
			 try {
				 conn.rollback();
				 
			 } catch(SQLException ex) {
				 throw new DAOException(ex.getMessage(), ex);
			 }
		 }
	 }
	 
	 /**
	  * close the process of query.
	  * @param ps
	  * @throws DAOException
	  */
	 public static void closeStatement(PreparedStatement ps) throws DAOException{
			
		 if(ps != null) {
			 try {
				 ps.close();
				
			 } catch(SQLException ex) { 
			    	
				 throw new DAOException(ex.getMessage(), ex);
				
			 }
		 }
	  }
	 /**
	  * close the process of the information recovery from database
	  * @param rs
	  * @throws DAOException
	  */
	 public static void closeResultSet(ResultSet rs) throws DAOException{
		 
		 if(rs != null){
			 try {
				 rs.close();
				 
			 } catch(SQLException ex) {
				 throw new DAOException(ex.getMessage(), ex);
			 }
		 }
	 }

}
